function VehicleStatsView() {

	var tableview = Titanium.UI.createTableView({
		style : Titanium.UI.iPhone.TableViewStyle.GROUPED
	});

	// The updateVehicleStatsEvent event basically sets up the entire table,
	// there was a more efficient approach previously but this one doesn't have the
	// intermittent problem of some text boxes not being populated, so we'll keep it like
	// this for the time being.
	tableview.addEventListener('updateVehicleStatsEvent', function(e) {

		// Clear tableview

		var data = [];

		// Fuel consumption section
		var fuelConsSection = Ti.UI.createTableViewSection({
			headerTitle : 'Fuel Consumption'
		});

		data.push(fuelConsSection);

		var fuelConsAverageRow = Ti.UI.createTableViewRow({
			height : 50
		});
		var fuelConsAverageLabel = Ti.UI.createLabel({
			text : 'average',
			font : {
				fontSize : 16,
				fontWeight : 'bold',
				fontFamily : 'Arial'
			},
			height : 'auto',
			width : 'auto',
			left : 10
		});
		var fuelConsAverageValue = Ti.UI.createLabel({
			color : '#336699',
			right : 15,
			font : {
				fontSize : 14,
				fontWeight : 'bold',
				fontFamily : 'Arial'
			},
			height : 'auto',
			width : 'auto'
		});

		fuelConsAverageRow.add(fuelConsAverageLabel);
		fuelConsAverageRow.add(fuelConsAverageValue);
		data.push(fuelConsAverageRow);

		var fuelConsChartRow = Ti.UI.createTableViewRow({
			height : 50
		});
		var fuelConsViewChartLabel = Ti.UI.createLabel({
			text : 'view chart',
			font : {
				fontSize : 16,
				fontWeight : 'bold',
				fontFamily : 'Arial'
			},
			height : 'auto',
			width : 'auto',
			left : 10
		});
		var fuelConsViewChartButton = Ti.UI.createButton({
			style : Titanium.UI.iPhone.SystemButton.DISCLOSURE,
			right : 10
		});
		fuelConsChartRow.add(fuelConsViewChartLabel);
		fuelConsChartRow.add(fuelConsViewChartButton);
		data.push(fuelConsChartRow);

		// Fuel economy section
		var fuelEconomySection = Ti.UI.createTableViewSection({
			headerTitle : 'Fuel Economy'
		});

		data.push(fuelEconomySection);

		var fuelEconomyAverageRow = Ti.UI.createTableViewRow({
			height : 50
		});
		var fuelEconomyAverageLabel = Ti.UI.createLabel({
			text : 'average',
			font : {
				fontSize : 16,
				fontWeight : 'bold',
				fontFamily : 'Arial'
			},
			height : 'auto',
			width : 'auto',
			left : 10
		});
		var fuelEconomyAverageValue = Ti.UI.createLabel({
			color : '#336699',
			right : 15,
			font : {
				fontSize : 14,
				fontWeight : 'bold',
				fontFamily : 'Arial'
			},
			height : 'auto',
			width : 'auto'
		});

		fuelEconomyAverageRow.add(fuelEconomyAverageLabel);
		fuelEconomyAverageRow.add(fuelEconomyAverageValue);
		data.push(fuelEconomyAverageRow);

		var fuelEconomyChartRow = Ti.UI.createTableViewRow({
			height : 50
		});
		var fuelEconomyViewChartLabel = Ti.UI.createLabel({
			text : 'view chart',
			font : {
				fontSize : 16,
				fontWeight : 'bold',
				fontFamily : 'Arial'
			},
			height : 'auto',
			width : 'auto',
			left : 10
		});
		var fuelEconomyViewChartButton = Ti.UI.createButton({
			style : Titanium.UI.iPhone.SystemButton.DISCLOSURE,
			right : 10
		});
		fuelEconomyChartRow.add(fuelEconomyViewChartLabel);
		fuelEconomyChartRow.add(fuelEconomyViewChartButton);
		data.push(fuelEconomyChartRow);

		// Cost per distance
		var cpdSection = Ti.UI.createTableViewSection({
			headerTitle : 'Costs'
		});

		data.push(cpdSection);

		var cpdAverageRow = Ti.UI.createTableViewRow({
			height : 50
		});
		var cpdAverageLabel = Ti.UI.createLabel({
			text : 'average cost per 1 km',
			font : {
				fontSize : 16,
				fontWeight : 'bold',
				fontFamily : 'Arial'
			},
			height : 'auto',
			width : 'auto',
			left : 10
		});
		var cpdAverageValue = Ti.UI.createLabel({
			color : '#336699',
			right : 15,
			font : {
				fontSize : 14,
				fontWeight : 'bold',
				fontFamily : 'Arial'
			},
			height : 'auto',
			width : 'auto'
		});

		cpdAverageRow.add(cpdAverageLabel);
		cpdAverageRow.add(cpdAverageValue);
		data.push(cpdAverageRow);

		var cpuAverageRow = Ti.UI.createTableViewRow({
			height : 50
		});
		var cpuAverageLabel = Ti.UI.createLabel({
			text : 'average cost per 1 L',
			font : {
				fontSize : 16,
				fontWeight : 'bold',
				fontFamily : 'Arial'
			},
			height : 'auto',
			width : 'auto',
			left : 10
		});
		var cpuAverageValue = Ti.UI.createLabel({
			color : '#336699',
			right : 15,
			font : {
				fontSize : 14,
				fontWeight : 'bold',
				fontFamily : 'Arial'
			},
			height : 'auto',
			width : 'auto'
		});

		cpuAverageRow.add(cpuAverageLabel);
		cpuAverageRow.add(cpuAverageValue);
		data.push(cpuAverageRow);

		var totalCostsRow = Ti.UI.createTableViewRow({
			height : 50
		});
		var totalCostsLabel = Ti.UI.createLabel({
			text : 'total fillup costs',
			font : {
				fontSize : 16,
				fontWeight : 'bold',
				fontFamily : 'Arial'
			},
			height : 'auto',
			width : 'auto',
			left : 10
		});
		var totalCostsValue = Ti.UI.createLabel({
			color : '#336699',
			right : 15,
			font : {
				fontSize : 14,
				fontWeight : 'bold',
				fontFamily : 'Arial'
			},
			height : 'auto',
			width : 'auto'
		});

		totalCostsRow.add(totalCostsLabel);
		totalCostsRow.add(totalCostsValue);
		data.push(totalCostsRow);

		var Conversions = require('tools/Conversions');

		// Upon updated vehicle ID, we by default reset all default values
		resetAll(fuelConsAverageValue, fuelEconomyAverageValue, cpdAverageValue, cpuAverageValue, totalCostsValue);

		// Then we set each value that is displayed in this view
		if(e.averageFuelConsumption && !isNaN(e.averageFuelConsumption)) {
			fuelConsAverageValue.setText(e.averageFuelConsumption + ' L/100 km');
			fuelConsAverageValue.show();
		}
		if(e.averageFuelEconomy && !isNaN(e.averageFuelEconomy)) {
			fuelEconomyAverageValue.setText(e.averageFuelEconomy + ' km/L');
			fuelEconomyAverageValue.show();
		}

		if(e.costPerDistance) {
			var costPerDistance = e.costPerDistance.toFixed(2);
			for(var i = 0; i < 10; i++) {
				cpdAverageValue.setText(e.costPerDistance.toFixed(2) + ' ' + Conversions.getCurrentCurrency());
			}
		}

		if(e.costPerUnit) {
			cpuAverageValue.setText(e.costPerUnit.toFixed(2) + ' ' + Conversions.getCurrentCurrency());
		}

		if(e.totalCosts) {
			totalCostsValue.setText(e.totalCosts.toFixed(2) + ' ' + Conversions.getCurrentCurrency());
		}

		currentVehicleId = e.vehicleId;

		// Set table view data
		tableview.setData(data);

		// View fuel consumption chart
		fuelConsChartRow.addEventListener('click', function(e) {
			tableview.fireEvent('wantToViewFuelConsumptionChart', {
				vehicleId : currentVehicleId
			});
		});

		fuelEconomyChartRow.addEventListener('click', function(e) {
			tableview.fireEvent('wantToViewFuelEconomyChart', {
				vehicleId : currentVehicleId
			});
		});
	});

	return tableview;
}

var currentVehicleId;

function resetAll(fuelConsAverageValue, fuelEconomyAverageValue, cpdAverageValue, cpuAverageValue, totalCostsValue) {
	currentVehicleId = null;
	fuelConsAverageValue.setText('');
	fuelEconomyAverageValue.setText('');
	cpdAverageValue.setText('');
	cpuAverageValue.setText('');
	totalCostsValue.setText('');
}

module.exports = VehicleStatsView; 